package io.renren.modules.app.controller;


import io.renren.common.exception.RRException;
import io.renren.common.utils.DateUtils;
import io.renren.common.utils.R;
import io.renren.common.validator.Assert;
import io.renren.modules.app.annotation.Login;
import io.renren.modules.app.service.UserService;
import io.renren.modules.sys.entity.CodeEntity;
import io.renren.modules.sys.entity.FeedbackEntity;
import io.renren.modules.sys.entity.UserEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * 用户模块
 * @author wangyi
 * @createdate 2018/3/15 11:23
 **/
@Api(description = "用户")
@RestController
@RequestMapping("/app")
public class ApiUserController {
    @Autowired
    private UserService userService;
    @Autowired
    private HttpServletRequest request;
    
    /**
     * 获取当前登陆用户信息
     * @author wangyi
     * @createdate 2018/3/19 15:28
     **/
    @ApiOperation("获取当前登陆用户信息 *{需要登陆的token}*")
    @Login
     @PostMapping("userInfo")
     public R userInfo(){
         Long userId=(Long)request.getAttribute("userId");
         UserEntity userEntity=userService.queryObject(userId);
        userEntity.setPassword(null);
         return R.ok().put("result",userEntity);
     }

    /**
     * 更改密码
     */
    @ApiOperation("更改密码 *{需要登陆的token}*")
    @Login
    @PostMapping("changePassword")
    public R changePassword(String password,String SMSCode){
        Assert.isBlank(password, "密码不能为空");
        Assert.isBlank(SMSCode, "短信验证码不能为空");
        Long userId=(Long)request.getAttribute("userId");
        UserEntity userEntity=userService.queryObject(userId);
        String mobile=userEntity.getMobile();
        CodeEntity codeEntity=userService.queryCodeByMobile(mobile);
        if(codeEntity==null){
            throw new RRException("请先获取验证码");
        }
        Long expire=codeEntity.getExpire();
        String code=codeEntity.getCode();
        if(DateUtils.getTime()>expire){
            throw new RRException("验证码已过期");
        }
        if(!SMSCode.equals(code)){
            throw new RRException("验证码错误");
        }
        userService.forgetPassword(userId,password);
        return R.ok("修改成功");
    }

    /**
     * 更改用户信息
     */
    @ApiOperation("更改用户信息 *{需要登陆的token}*")
    @Login
    @PostMapping("updateUser")
    public R updateUser(String username, String img, Integer sex){
        Long userId=(Long)request.getAttribute("userId");
        UserEntity user=new UserEntity();
        if(StringUtils.isNotBlank(username)){
            user.setUsername(username);
            user.setUserId(userId);
        }
        if(StringUtils.isNotBlank(img)){
            user.setImg(img);
            user.setUserId(userId);
        }
        if(sex!=null){
            user.setSex(sex);
            user.setUserId(userId);
        }
        if(user.getUserId()!=null){
            userService.update(user);
        }else{
            throw new RRException("请填写修改内容");
        }
        return R.ok("修改成功");
    }

    /**
     * 用户反馈
     * @author wangyi
     * @createdate 2018/3/19 16:03
     **/
    @ApiOperation("用户反馈 *{需要登陆的token}*")
    @Login
     @PostMapping("feedback")
     public R feedback(String type,String content){
        Long userId=(Long)request.getAttribute("userId");
        FeedbackEntity feedbackEntity=new FeedbackEntity();
        feedbackEntity.setUserId(userId);
        feedbackEntity.setType(type);
        feedbackEntity.setContent(content);
        feedbackEntity.setCreateTime(new Date());
        userService.feedback(feedbackEntity);
         return R.ok();
     }
}
